package com.onluy.framework.system.resolver;


import java.util.Iterator;
import org.springframework.core.MethodParameter;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import com.onluy.framework.data.util.InParam; 

public class InParamMethodArgumentResolver extends BaseMethodArgumentResolver
  implements HandlerMethodArgumentResolver
{
  public boolean supportsParameter(MethodParameter parameter)
  {
    return InParam.class.isAssignableFrom(parameter.getParameterType());
  }

  public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception
  {
    InParam inParam = new InParam();
    Iterator fields = webRequest.getParameterNames();
    while (fields.hasNext()) {
      String field = (String)fields.next();
      String[] values = webRequest.getParameterValues(field);
      if (values.length > 1) {
        inParam.put(field, values);
      }
      else if (!StringUtils.isEmpty(values[0])) {
        inParam.put(field, values[0]);
      }

    } 
    return inParam;
  }
}